<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100px;
        height: 100px;
        background-color: hotpink;
        position: absolute;
        left: 0px;
        top: 0px;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
    <script>
      var boxEle = document.querySelector(".box");
      function move(item, target, dir, cb) {
        var speen = target < num ? 1 : -1;
        function fn() {
          var num = parseInt(getComputedStyle(item)[dir]);
          if (num == target) {
            console.log("停住");
            cb && cb();
          } else {
            num += speen;
            item.style[dir] = num + "px";
            requestAnimationFrame(fn);
          }
        }
        fn();
      }
      move(boxEle, 300, "left");
    </script>
  </body>
</html>
